package com.zbcbms.service.impl;

import com.zbcbms.dao.BookBorrowDao;
import com.zbcbms.dao.BookDao;
import com.zbcbms.dao.impl.BookBorrowDaoImpl;
import com.zbcbms.dao.impl.BookDaoImpl;
import com.zbcbms.entity.Book;
import com.zbcbms.entity.tableData.BookTableData;
import com.zbcbms.entity.chart.BookTypeChart;
import com.zbcbms.entity.vo.BookVo;
import com.zbcbms.service.BookService;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.util.List;

/**
 * @author fengq
 */
public class BookServiceImpl implements BookService {

    private BookDao bookDao=new BookDaoImpl();

    private BookBorrowDao bookBorrowDao=new BookBorrowDaoImpl();

    @Override
    public Boolean saveOne(Book entity) {
        return bookDao.insertOne(entity);
    }

    @Override
    public Boolean updateById(Book entity) {
        String id = bookDao.selectIdByNumber(entity.getBookNumber());
        entity.setId(id);
        return bookDao.update(entity);
    }

    @Override
    public Boolean deleteById(String id) {
        bookBorrowDao.updateDeletedByBookNumber(id,1);
        return bookDao.deleteById(id);
    }

    @Override
    public int deleteByIds(List<String> ids) {
        return bookDao.deleteByIds(ids);
    }

    @Override
    public Boolean pjySicsDeleteById(String id) {
        return bookDao.pjySicsDeleteById(id);
    }

    @Override
    public Boolean recoverById(String id) {
       bookBorrowDao.updateDeletedByBookNumber(id,0);
       return bookDao.recoverById(id);
    }

    @Override
    public ObservableList<BookTableData> getBookTableViewData() {
        List<BookVo> list = bookDao.selectAllBook();
        return conversion(list);
    }

    @Override
    public ObservableList<String> getAllBookNumberWithStatus() {
        List<String> stringList = bookDao.selectAllBookNumberWithStatus();
        ObservableList<String> list=FXCollections.observableArrayList();
        for (String s:stringList){
            list.add(s);
        }
        return list;
    }

    @Override
    public ObservableList<String> getBookNumberByKeywordWithStatus(String keyWord) {
        List<String> stringList = bookDao.selectNumberByKeywordWithStatus(keyWord);
        ObservableList<String> list=FXCollections.observableArrayList();
        for (String s:stringList){
            list.add(s);
        }
        return list;
    }

    @Override
    public String getBookNameByNumber(String number) {
        return bookDao.selectBookNameByNumber(number);
    }

    @Override
    public long getCount(String status) {
        return bookDao.selectCount(status);
    }

    @Override
    public List<BookTypeChart> getCountByType() {
        return bookDao.selectBookCountByType();
    }

    @Override
    public ObservableList<BookTableData> getAllDeletedUser() {
        List<BookVo> list = bookDao.selectAllDeletedBook();
        return conversion(list);
    }

    @Override
    public ObservableList<BookTableData> getBookVoByKeyWord(String keyword) {
        List<BookVo> list = bookDao.selectBookVoByKeWord(keyword);
        return conversion(list);
    }

    public ObservableList<BookTableData> conversion( List<BookVo> bookVos){
        ObservableList<BookTableData> tableData= FXCollections.observableArrayList();
        for (BookVo bookVo:bookVos){
            BookTableData bookTableData = new BookTableData(bookVo.getBookNumber(),bookVo.getBookName(),
                    bookVo.getStatus(),bookVo.getPublishingHouse(),bookVo.getPrice(),bookVo.getPublishingTime()
                    ,bookVo.getAuthor(),bookVo.getType());
            tableData.add(bookTableData);
        }
        return tableData;
    }


}
